<template>
  <el-card class="form-container" shadow="never">
    <h3
      style="font-family: 'Helvetica Neue',Helvetica,'PingFang SC','Hiragino Sans GB','Microsoft YaHei','微软雅黑',Arial,sans-serif;color: #1f2f3d;font-weight: 400;text-align: left">
      <i class="el-icon-tickets"></i>
      <span>修改商城信息</span>
    </h3>

    <el-form :model="info"
             ref="form"
             label-width="150px" size="small">

      <el-form-item label="联系电话：">
        <el-input v-model="info.tel" style="width: 250px"></el-input>
      </el-form-item>

      <el-form-item label="联系邮箱：">
        <el-input v-model="info.mail" style="width: 250px"></el-input>
      </el-form-item>

      <el-form-item label="联系QQ：">
        <el-input v-model="info.qq" style="width: 250px"></el-input>
      </el-form-item>

      <el-form-item label="联系微信二维码：">
        <el-upload
          class="avatar-uploader"
          action="http://wlgc5.oss-cn-shanghai.aliyuncs.com"
          :show-file-list="false"
          :data="dataObj"
          :on-success="handleUploadSuccessWeChat"
          :before-upload="beforeUpload">
          <img v-if="info.wechat" :src="info.wechat" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="微博二维码：">
        <el-upload
          class="avatar-uploader"
          action="http://wlgc5.oss-cn-shanghai.aliyuncs.com"
          :show-file-list="false"
          :data="dataObj2"
          :on-success="handleUploadSuccessWeiBo"
          :before-upload="beforeUpload2">
          <img v-if="info.weibo" :src="info.weibo" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

    </el-form>
    <div ref="t" style="width: 100%;display: flex;justify-content: center;flex-direction: row">
      <div>
        <el-button style="margin: 0 auto;width: fit-content" type="primary" @click.prevent.native="handle">提交
        </el-button>
      </div>
    </div>
  </el-card>
</template>

<script>

import {policy} from "@/api/oss";
import {getInfo, updateInfo} from "@/api/info";

const defaultInfo = {
  id: null,
  tel: null,
  mail: null,
  qq: null,
  wechat: null,
  weibo: null,
}

export default {
  name: "info",
  data() {
    return {
      info: Object.assign({}, defaultInfo)
      , dataObj: {}
      , dataObj2: {}
    }
  },
  created() {
    getInfo().then(
      res => {
        if (res.data) {
          this.info = res.data
        }
      }
    )
  },
  methods: {
    handle() {
      updateInfo(this.info).then(res => {
        this.$message.success(res.message)
      })
    },
    beforeUpload(file) {
      // const isImg = file.type === 'image/jpeg';
      const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);

      const whiteList = ["png", "jpeg", "jpg"];

      if (whiteList.indexOf(fileSuffix) === -1) {
        this.$message.error("上传文件只能是" + whiteList.join(',') + "格式");
        return false;
      }

      const isLt2M = file.size / 1024 / 1024 < 2;


      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
        return false;
      }

      let _self = this;

      return new Promise((resolve, reject) => {
        policy().then(response => {
          _self.dataObj.policy = response.data.policy;
          _self.dataObj.signature = response.data.signature;
          _self.dataObj.ossaccessKeyId = response.data.accessKeyId;
          _self.dataObj.key = response.data.dir + '/${filename}';
          _self.dataObj.dir = response.data.dir;
          _self.dataObj.host = response.data.host;
          // _self.dataObj.callback = response.data.callback;
          resolve(true)
        }).catch(err => {
          console.log(err)
          reject(false)
        })
      })
    },
    beforeUpload2(file) {
      // const isImg = file.type === 'image/jpeg';
      const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);

      const whiteList = ["png", "jpeg", "jpg"];

      if (whiteList.indexOf(fileSuffix) === -1) {
        this.$message.error("上传文件只能是" + whiteList.join(',') + "格式");
        return false;
      }

      const isLt2M = file.size / 1024 / 1024 < 2;


      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
        return false;
      }

      let _self = this;

      return new Promise((resolve, reject) => {
        policy().then(response => {
          _self.dataObj2.policy = response.data.policy;
          _self.dataObj2.signature = response.data.signature;
          _self.dataObj2.ossaccessKeyId = response.data.accessKeyId;
          _self.dataObj2.key = response.data.dir + '/${filename}';
          _self.dataObj2.dir = response.data.dir;
          _self.dataObj2.host = response.data.host;
          // _self.dataObj.callback = response.data.callback;
          resolve(true)
        }).catch(err => {
          console.log(err)
          reject(false)
        })
      })
    },
    handleUploadSuccessWeChat(res, file) {
      console.log(res)
      this.info.wechat = this.dataObj.host + '/' + this.dataObj.dir + '/' + file.name
    },

    handleUploadSuccessWeiBo(res, file) {
      console.log(res)
      this.info.weibo = this.dataObj2.host + '/' + this.dataObj2.dir + '/' + file.name
    }

  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
